package cn.vser.server.demo.entity;

import java.util.ArrayList;
import java.util.List;

import cn.vser.common.utils.CodeUtils;
import cn.vser.server.ApiException;
import cn.vser.server.VserApi;
import cn.vser.server.entity.StmaddrVo;

public class DataSim {

    //public static List<User> userList;
    public static List<Room> roomList;

    static {
        roomList = new ArrayList<>();
        Room r1 = new Room();
        r1.setUid("1");
        r1.setScode("hely");
        r1.setRname("Hely导播");
        Room r2 = new Room();
        r2.setUid("2");
        r2.setScode("99e2cbed2bbd46a3af5780fd24dc7364");
        r2.setRname("Wojj直播间");
        roomList.add(r1);
        roomList.add(r2);
    }

    public static Room getRoomByUid(String uid, boolean flag, String rname) {
        if (rname != null && !rname.equals("")) {
            rname = "直播"+ rname;
        } else {
            rname = "直播"+ uid;
        }
        for (Room r : roomList) {
            if (r.getUid().equals(uid)) {
                if (flag)
                r.setRname(rname);
                return r;
            }
        }
        if (!flag) return null;
        String scode = CodeUtils.sha1(uid);
        Room r = new Room();
        r.setUid(uid);
        r.setRname(rname);
        r.setScode(scode);
        roomList.add(r);
        return r;
    }
    
    public static List<Room> getRooms() throws ApiException {
        List<Room> rooms = new ArrayList<>();
        List<StmaddrVo> stmaddrs  = VserApi.getInstance().getStmaddrs(null);
        for (Room r : roomList) {
            StmaddrVo stm = getStmaddr(stmaddrs, r.getScode());
            if (stm != null && stm.getState()) {
                rooms.add(r);
            }
        }
        for (StmaddrVo s : stmaddrs) {
            Room r = getRoom(rooms, s.getScode());
            if (r == null) {
                r = new Room();
                r.setUid(s.getScode());
                if (s.getSname() == null) {
                    r.setRname("无名");
                } else {
                    r.setRname(s.getSname());
                }
                r.setScode(s.getScode());
                rooms.add(r);
                roomList.add(r);
            }
        }
        return rooms;
    }
    
    private static StmaddrVo getStmaddr(List<StmaddrVo> stmaddrs, String scode) {
        for (StmaddrVo s : stmaddrs) {
            if (s.getScode().equals(scode)) {
                return s;
            }
        }
        return null;
    }
    
    private static Room getRoom(List<Room> rooms, String scode) {
        for (Room r : rooms) {
            if (scode.equals(r.getScode())) {
                return r;
            }
        }
        return null;
    }
}
